(function($){
	//编辑标准编辑界面添加样式
	if($(".form-action-panel").length || $(".form-actions>.col-md-offset-5").length){
		$("body").addClass("eidt-page")
	}
	//默认字号适配
	if($(window).width() > 1600) $("html").css("font-size", '14px');
	$(window).resize(function(){
		if($(window).width() > 1600) $("html").css("font-size", '14px');
	});
	
	//表格滚动条设置
	$.fn.datagrid.defaults.scrollbarSize = 0;
	$.fn.treegrid.defaults.scrollbarSize = 0;
	$.fn.treegrid.defaults.scrollbarSize = 0;
	$.fn.treegrid.defaults.scrollbarSize = 0;
	
	//处理表格序号列 start
	$.fn.datagrid.defaults.isRownumbers = false;
	function setRowNum(options){
		if(typeof options == "string" || !options) return;
		var rowNumberCol = {
			field: '$number',title: '序号',align: "center",width: 60, fixed: true, resizable: false, formatter: function (data, row, index) {
				return index +1;
			}
		};
		var columns, hasRownumber = false;
		if(Array.isArray(options.frozenColumns) && options.frozenColumns.length && options.frozenColumns[0].length){
			columns = options.frozenColumns[0];
			for(var i = 0; i < columns.length; i++) {
				if(columns[i].title == "序号"){
					columns[i].width = 60;
					columns[i].fixed = true;
					columns[i].resizable = false;
					hasRownumber = true;
					break;
				}
			}
		} 
		if(!hasRownumber && Array.isArray(options.columns) && options.columns.length && options.columns[0].length) {
			columns = options.columns[0];
			for(var i = 0; i < columns.length; i++) {
				if(columns[i].title == "序号"){
					hasRownumber = true;
					columns[i].width = 60;
					columns[i].fixed = true;
					columns[i].resizable = false;
					break;
				}
			}
		}
		options.isRownumbers = false;
		if(options.rownumbers && !hasRownumber) {
			options.isRownumbers = true;
			if(!options.isRightFixed && Array.isArray(options.frozenColumns) && options.frozenColumns.length && options.frozenColumns[0].length){
				columns = options.frozenColumns[0];
			} else if(Array.isArray(options.columns) && options.columns.length && options.columns[0].length) {
				columns = options.columns[0];
			}
			if(columns){
				if(columns[0].checkbox){
					columns.splice(1,0,rowNumberCol)
				} else {
					columns.unshift(rowNumberCol)
				}
			}
		}
		options.rownumbers = false;
	}
	var enableFilter = $.fn.datagrid.methods.enableFilter;
	$.fn.datagrid.methods.enableFilter = function(jq, filters){
		jq.each(function(){
			var opts = $.data(this, "datagrid").options;
			if(opts.isRownumbers) {
				var hasRownumber = false;
				for(var i = 0; i < filters.length; i++) {
					if(filters[i].field == "$number"){
						hasRownumber = true;
						break;
					}
				}
				if(!hasRownumber) {
					filters.push({"field": "$number", type: 'label'})
				}
			}
		})
		return enableFilter.call(this, jq, filters);
	}
	//处理表格序号列 end
	
	//拖动列宽的处理
	function dragCol(options){
		if(typeof options == "string" || !options) return;
		$(this).parent().find(".datagrid-header-row td").each(function(){
			var resizable = $(this).find('div[resizable="false"]');
			if(resizable.length) {
				$(this).attr("resizable",false);
			}
		});
	}
	
	var datagrid = $.fn.datagrid;
	$.fn.datagrid = function(options, param){
		setRowNum.call(this,options);
		var res = datagrid.call(this,options, param);
		dragCol.call(this,options);
		return res;
	}
	$.fn.datagrid = Object.assign($.fn.datagrid,datagrid);
	/**
	 * 计算table的高度
	 * @param {Object} containerId 容器Id 用于多个表格在同一页时操作用的
	 * @param {Object} extraHeight 需要额外减去的高度 不能是百分数
	 */
	App.countHeight = function (containerId, extraHeight) {
		if(containerId) containerId = "#" + containerId + " ";
		else containerId = "";
		if(!extraHeight) extraHeight = 0;
		else extraHeight = parseFloat(extraHeight);
		var bodyHeight = $('body').outerHeight(true) - extraHeight;
		var customTitle = $(containerId + '.custom-title').outerHeight(true);
		if (!customTitle) customTitle = 0;
		var searchHeight = $(containerId + '.list-search').outerHeight(true);
		if (!searchHeight) searchHeight = 0;
		var btnGroupHeight = $(containerId + '.list-btn-row').outerHeight(true);
		if (!btnGroupHeight) btnGroupHeight = 0;
		var formActionPanel = $(containerId + '.form-action-panel').outerHeight(true);
		if (!formActionPanel || $(containerId + '.page-content-form .form-action-panel').length > 0) formActionPanel = 0;
		var tableHeight = bodyHeight - customTitle - searchHeight - btnGroupHeight - formActionPanel - 26;
		$(containerId + '.table-wrapper').css('height', tableHeight);
		return tableHeight;
	};
})(jQuery)